import {
  adapterUtils,
  IDCardUtils,
} from "../../../../utils/index.js";
import dayjs from "dayjs";
import cloneDeep from "lodash/cloneDeep.js";
import pick from "lodash/pick.js";
import customParseFormat from "dayjs/plugin/customParseFormat.js";
import __mainDataStore from './constants/mainDataStore.js'
import __detailDataStore from './constants/detailDataStore.js'
import __guideDataStore from './constants/guideDataStore.js'
// import mainDataStore from './constants/mainDataStore.js'

dayjs.extend(customParseFormat);
const { setPersonMessage } = adapterUtils;

const physiquetypeArr = [
  { physiquetype: '气虚质', scoreArr: [2, 3, 4, 14] },
  { physiquetype: '阳虚质', scoreArr: [11, 12, 13, 29] },
  { physiquetype: '阴虚质', scoreArr: [10, 21, 26, 31] },
  { physiquetype: '痰湿质', scoreArr: [9, 28, 16, 32] },
  { physiquetype: '湿热质', scoreArr: [23, 25, 27, 30] },
  { physiquetype: '血瘀质', scoreArr: [19, 22, 24, 33] },
  { physiquetype: '气郁质', scoreArr: [5, 6, 7, 8] },
  { physiquetype: '特禀质', scoreArr: [15, 17, 18, 20] },
  { physiquetype: '平和质', scoreArr: [2, 4, 5, 13] }, // 1 是正向计分 [2, 4, 5, 13]反向计分（6-x)
];

export function chineseMedicineAdapter(person, code, guideArr, hisConfigSession) {
  const scoreIterator = adapterUtils.getNameIterator({ end: 33, prefix: 'assessment' });
  const scoreObj = pick(person, [...scoreIterator]);

  let _mainDataStore = cloneDeep(__mainDataStore);
  let _detailDataStore = cloneDeep(__detailDataStore);
  let _guideDataStore = cloneDeep(__guideDataStore);

  _mainDataStore = {
    ..._mainDataStore,
    ...scoreObj,
    assessdate: dayjs(person.assessdate).unix() * 1000,
    assessdoctorcode: hisConfigSession.UserId,
    code,
    card: code,
    assessdoctor: hisConfigSession.UserName,
  };

  const { detailDataStore, tz } = formatDetailDataStore(_detailDataStore, scoreObj);
  _detailDataStore = detailDataStore;

  const guideObj = getGuideObj(guideArr, tz);
  _guideDataStore = formatGuideDataStore(_guideDataStore, guideObj);
  // console.log(_guideDataStore);

  return { _mainDataStore, _detailDataStore, _guideDataStore, person };
}

function formatDetailDataStore(detailDataStore, scoreObj) {
  let isLess8 = true;
  let isLess10 = true;
  let tz = '';
  const _detailDataStore = detailDataStore.map(first => {
    const _first = cloneDeep(first);
    const { scoreArr } = physiquetypeArr.find(second => first.physiquetype === second.physiquetype);
    if (first.physiquetype !== '平和质') {
      _first.score = scoreArr.reduce((sum, cur) => {
        sum += Number(scoreObj[`assessment${cur}`]);
        return sum;
      }, 0);
      if (_first.score > 10) {
        if (!tz) tz = first.physiquetype;
        isLess10 = false;
        _first.identifycode = '2'; // 是
        _first._o = { score: null, identifycode: null };
      } else if (_first.score > 8) {
        isLess8 = false;
        _first.identifycode = '3'; // 倾向是
        _first._o = { score: null, identifycode: null };
      }
    } else {
      _first.score = scoreArr.reduce((sum, cur) => {
        sum += 6 - Number(scoreObj[`assessment${cur}`]);
        return sum;
      }, Number(scoreObj.assessment1));
      if (isLess8 && isLess10 && _first.score >= 17) {
        if (!tz) tz = first.physiquetype;
        _first.identifycode = '2'; // 是
        _first._o = { score: null, identifycode: null };
      } else if (!isLess8 && isLess10 && _first.score >= 17) {
        _first.identifycode = '3'; // 倾向是
        _first._o = { score: null, identifycode: null };
      }
    }
    return _first
  });
  return { detailDataStore: _detailDataStore, tz };
}

function formatGuideDataStore(guideDataStore, guideObj) {
  let _guideDataStore = cloneDeep(guideDataStore);
  _guideDataStore = _guideDataStore.map(item => {
    const key = item.guidetype.substr(0, 2);
    if (guideObj[key]) item.guideinfo = guideObj[key];
    return item;
  });
  return _guideDataStore;
}

/**
 * 根据接口和体质返回体质对应的评价
 * @param conditionArr 接口返回的primary数组
 * @param tz 患者体质（平和质，平和，血瘀质，血瘀）等
 * @returns {*}
 */
function getGuideObj(conditionArr, tz) {
  const tzObj = conditionArr.find(
    item => item.tpecontent.includes(tz) && item.tpecontent.length === 3
  );
  const suggestArr = conditionArr.filter(item => item.parentid === tzObj.tpeid);
  const suggestObj = suggestArr.reduce((pre, cur) => {
    const [key, val] = cur.tpecontent.split(/^([^：]*)：/).filter(item => item);
    if (key) {
      pre[key.substr(0, 2)] = val;
    }
    return pre;
  }, {});

  return suggestObj;
}
